<?
include_once "DlgPackagesBase.class.php" ;
include_once "DlgClass.class.php" ;
include_once "JCAT_ItemSelectedIterator.class.php" ;

$hGlobalSqlite = sqlite_open('./database/PackagesClasses.sqlite',0644) ;
		
class DlgPackages extends DlgPackagesBase
{
	function DlgPackages()
	{
		$this->DlgPackagesBase() ;
		
		$this->InitWidget() ;

		
		// 视图
		$this->aModelTreePackages = new GtkTreeStore( JCAT_Gtk_TYPE_BOOLEAN, JCAT_Gtk_TYPE_STRING, JCAT_Gtk_TYPE_STRING, JCAT_Gtk_TYPE_STRING, JCAT_Gtk_TYPE_STRING, JCAT_Gtk_TYPE_STRING );
		$this->wndTreePackages->set_model($this->aModelTreePackages) ;
		$aRenderer = new GtkCellRendererText();
		$aRendererBox = new GtkCellRendererToggle();
		$aRendererBox->connect('toggled',array($this,'OnClassToggled')) ;
		$this->wndTreePackages->append_column(new GtkTreeViewColumn('Op',$aRendererBox, 'active', 0)) ;
		$this->wndTreePackages->append_column(new GtkTreeViewColumn('Name',$aRenderer, 'text', 1)) ;
		$this->wndTreePackages->append_column(new GtkTreeViewColumn('Type',$aRenderer, 'text', 2)) ;
		$this->wndTreePackages->append_column(new GtkTreeViewColumn('Package ID',$aRenderer, 'text', 3)) ;
		$this->wndTreePackages->append_column(new GtkTreeViewColumn('Class ID',$aRenderer, 'text', 4)) ;
		$this->wndTreePackages->append_column(new GtkTreeViewColumn('ID',$aRenderer, 'text', 5)) ;
		
		
		$this->connect_simple('destroy', array('gtk', 'main_quit')) ;	
		
		$this->Load() ;
	}

	public function Load()
	{
		
		$hRes = sqlite_query($GLOBALS['hGlobalSqlite'],'select * from classes where nId>0 order by nId') ;
		
		$this->arrPackagesAndClasses = array() ;
		$arrRows = sqlite_fetch_all($hRes) ;//print_r($arrRows) ;
		$this->aModelTreePackages->clear() ;
		foreach($arrRows as $arrRow)
		{
			$this->arrPackagesAndClasses[$arrRow['nId']] = $this->aModelTreePackages->append($this->arrPackagesAndClasses[$arrRow['nParentPackage']],array(
								false ,
								($arrRow['bClass']?'      ':'').$arrRow['sName'] ,
								$arrRow['bClass']? '<:Class:>': '{Package}' ,
								($arrRow['nId']>>8) ,
								($arrRow['nId']&255) ,
								$arrRow['nId'] ) ) ;
		}
		
		$this->wndTreePackages->expand_all() ;
	}
	

	
	// 定义按钮事件
	public function OnBtnNew()
	{
		$wndDlg = new DlgClass($this) ;
		$wndDlg->run() ;
	}

	public function OnBtnModify()
	{
	}

	public function OnBtnDelete()
	{
		$aDlg = new GtkMessageDialog(null, Gtk::DIALOG_MODAL, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_YES_NO, "确定删除？" );
		$aDlg->set_markup( "确定删除及其下级？" );
		$nAnswer = $aDlg->run();
		$aDlg->destroy();

		if($nAnswer!=Gtk::RESPONSE_YES)
			return ;
			
			
		$aIterator = new JCAT_ItemSelectedIterator(0,$this->aModelTreePackages) ;
		$aIterator->First() ;
		
		while( !$aIterator->IsDone() )
		{
			$aItem = $aIterator->Next() ;
			$nID = $this->aModelTreePackages->get_value($aItem,5) ;
			
			// 删除下级
			sqlite_query($GLOBALS['hGlobalSqlite'],"delete from classes where nParentPackage=$nID") ;
			
			// 删除
			sqlite_query($GLOBALS['hGlobalSqlite'],"delete from classes where nId=$nID") ;
		}
		
		$this->Load() ;
	}
	
	public function OnClassToggled( GtkCellRendererToggle $aRender, $sPath )
	{
		$aItem = $this->aModelTreePackages->get_iter($sPath) ;
		
		$bValue = !$this->aModelTreePackages->get_value($aItem,0) ;
		$this->aModelTreePackages->set($aItem,0,$bValue) ;		
	}
	
	public function OnBtnCode()
	{
		$sOut = '' ;
		$sOutPackagePath = '' ;
		
		
		// Packages
		$arrPackages = array() ;
		$hRes = sqlite_query($GLOBALS['hGlobalSqlite'],'select * from classes where nId>0 and bClass=0') ;
		$arrRows = sqlite_fetch_all($hRes) ;//print_r($arrRows) ;
		foreach($arrRows as $arrRow)
		{
			$arrPackages[ $arrRow['nId'] ] = array(
					'Name' => $arrRow['sName'] ,
					'Classes' => array() 
			) ;
			
			$nPackageID = ($arrRow['nId']>>8) ;
			
			$sOut.= "	const {$arrRow['sName']} = {$nPackageID} ;\r\n" ;
			$sOutPackagePath.= "		self::{$arrRow['sName']} => 'lib.php/' ,\r\n" ;
		}
		$sOut.= "\r\n\r\n" ;
		$sOut.= "	static private \$arrPackagesPath = array(\r\n" ;
		$sOut.= $sOutPackagePath ;
		$sOut.= "	) ;" ;

		// Classes
		$hRes = sqlite_query($GLOBALS['hGlobalSqlite'],'select * from classes where nId>0 and bClass=1') ;
		$arrRows = sqlite_fetch_all($hRes) ;print_r($arrRows) ;
		foreach($arrRows as $arrRow)
			$arrPackages[ $arrRow['nParentPackage'] ] ['Classes'][$arrRow['nId']] = $arrRow['sName'] ;
		
		foreach($arrPackages as $nPackageID=>$arrPackage)
		{
			$nPackageID = $nPackageID>>8 ;
			
			
			$sOut.= "\r\n\r\n\r\n" ;
			$sOut.= "包 {$arrPackage['Name']}：\r\n\r\n" ;
			foreach($arrPackage['Classes'] as $nClassId=>$sClassName)
			{
				$nClassReadId = ($nClassId&255) ;
				$sOut.= "JCAT_Package::RegisterClassID('{$sClassName}',JCAT::{$arrPackage['Name']},{$nClassReadId}) ; // {$nClassId}\r\n" ;
			}
		}
		
		// Saving ... ...
		echo $sOut ;
		file_put_contents('./code.php',$sOut) ;
	}

	public function OnBtnQuit()
	{gtk::main_quit() ;}
	
	private $arrPackagesAndClasses = array() ;
}
?>